package ru.spbu.math.android.cbrf.model;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Model for keeping data
 *
 * @author Svetlana Zemlyanskaya
 */

public class Rate {

    private static final String TAG = "Exception";

   StockItem source; // StockItem from StockItemStore
   StockItem target; //rub
   double value; //29.4003 or something else
   Date date; //date

    public Rate() {
        source = new StockItem();
        value = 0;
        target = new StockItem();
        date = new Date();
    }

    public Rate(String sourceCode, String targetCode, int value, Date date) {
        StockItemStore itemStock = StockItemStore.Instance();
        source = itemStock.findByCode(sourceCode);
        target = itemStock.findByCode(targetCode);
        this.value = value;
        this.date = date;
    }

    public Rate(String sourceCode, int value, Date date) {

        StockItemStore itemStock = StockItemStore.Instance();
        source = itemStock.findByCode(sourceCode);
        target = itemStock.findByCode("RUB");
        this.value = value;
        this.date = date;
    }

    public Rate(String sourceCode, String targetCode, double value, Date date) {
        StockItemStore itemStock = StockItemStore.Instance();
        source = itemStock.findByCode(sourceCode);
        target = itemStock.findByCode(targetCode);
        this.value = value;
        this.date = date;
    }

    public Rate(String sourceCode, double value, Date date) {
        StockItemStore itemStock = StockItemStore.Instance();
        source = itemStock.findByCode(sourceCode);
        target = itemStock.findByCode("RUB");
        this.value = value;
        this.date = date;
    }

    public StockItem getSource() {
        return source;
    }

    public double getValue() {
        return value;
    }

    public StockItem getTarget() {
        return target;
    }

    public Date getDate() {
        return date;
    }

    public void setSource(StockItem itemS) {
        source = itemS;
    }

    public void setValue(double valueF) {
        value = valueF;
    }

    public void setTarget(StockItem itemT) {
        target = itemT;
    }

    public void setDate(Date currDate) {
        date = currDate;
    }

    public void setDate(String currDate, int a) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            date = df.parse(currDate);
        } catch (ParseException e) {
            date = Calendar.getInstance().getTime();
            Log.e(TAG, "YANDEX.Parse Date Exception");
        }
    }
    
    public void setDate(String currDate) {
        DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
        try {
            date = df.parse(currDate);
        } catch (ParseException e) {
            date = Calendar.getInstance().getTime();
            Log.e(TAG, "CBRF.Parse Date Exception");
        }
    }

    public String toString() {

        String str = "";
        str += "Source: " + source.toString();
        str += "Target: " + target.toString();
        str += " Value: " + value;
        str += " Date: " + date;

        return str;
    }
}
